class List {
    constructor() {
      

      this.getData();
      this.bindEve();
    }
    bindEve() {
      // 给 ul 绑定点击事件
      this.$('.sk_bd ul').addEventListener('click', this.checkLogin.bind(this))
    }
    async getData() {
    //1 发送ajax请求获取数据
      let { status, data } = await axios.get('http://localhost:8888/goods/list');
      // console.log(goodsData);
      // console.log(status, data);
  
      // 2 判断请求的状态是否成功
      if (status != 200 && data.code != 1) throw new Error('获取数据失败...');
    // 3 循环渲染数据,追加到页面中
      let html = '';
      data.list.forEach(goods => {
        //  console.log(goods);
        html += `<li class="sk_goods" data-id="${goods.goods_id}">
        <a href="#none">
            <img src="${goods.img_big_logo}" alt="">
        </a>
        <h5 class="sk_goods_title">${goods.title}</h5>
        <p class="sk_goods_price">
            <em>¥${goods.current_price}</em>
            <del>￥${goods.price}</del>
        </p>
        <div class="sk_goods_progress">
            已售
            <i>${goods.sale_type}</i>
            <div class="bar">
                <div class="bar_in"></div>
            </div>
            剩余
            <em>29</em>件
        </div>
        <a href="#none" class="sk_goods_buy">立即抢购</a>
    </li>`;
      });
  
      // console.log(html);
      // 将拼接好的字符串,追加到ul中
      // console.log(this.$('.sk_bd ul'));
      this.$('.sk_bd ul').innerHTML += html;
    }
    
    checkLogin(eve) {//加入购物车
      if (eve.target.nodeName != 'A' || eve.target.className != 'sk_goods_buy') return;
      // console.log(eve.target);
      let token = localStorage.getItem('token');
      // console.log(token);
      if (!token) location.assign('./login.html?ReturnUrl=./list.html')
      let goodsId=eve.target.parentNode.dataset.id;
      // console.log(goodsId);
      let userId=localStorage.getItem('user_id');

      this.addCartGoods(goodsId,userId);
    }
   addCartGoods(gId,uId){
    //  console.log(gId,uId);
    const AUTH_TOKEN=localStorage.getItem('token')
    axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
    axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    let param=`id=${uId}&goodsId=${gId}`;
    axios.post('http://localhost:8888/cart/add',param).then(({data,status})=>{
    //  console.log(data,status);
    if(status==200 && data.code==1){
      layer.open({
        title: '添加成功'
        ,content: '去购物车查看商品吗？'
        ,btn:['留下','去吧']
        ,btn2:function(index,layer){
          location.assign('./cart.html')
        }
      });
    }else if(status == 200 && data.code == 401){
      localStorage.removeItem('token');
      localStorage.removeItem('user_id');
      //跳转到登录页面
      location.assign('./login.html?ReturnUrl=./list.html')
    } else {
      layer.open({
        title: '失败提示框'
        ,content: '添加失败'
        ,time:3000
        
      });
    }
    
    })

   }

    // 封装获取节点的方法
    $(ele) {
      let res = document.querySelectorAll(ele);
      return res.length == 1 ? res[0] : res;
    }
  }
  
  new List;